package com.wg.controller;

import com.wg.model.Student;
import org.springframework.web.bind.annotation.*;

/**
 * @Description:@RestController与@Controller区别
 * @Author WuGang
 * @Date 2021/10/15 19:05
 * @Version 1.0
 */
@RequestMapping(value = "/say")
@RestController//如果这个类所有方法返回的都是JSON那么方法上面就不用@ResponseBody类上面也不用加@controller，类似于@service把该类bean给spring管理
public class StudentController {
    @RequestMapping("/say1")
    public Object say1() {
        Student student = new Student();
        student.setAge(18);
        return student;
    }

    //    @RequestMapping(value = "/say2",method = {RequestMethod.PUT})//PUT是改 用postman来测  浏览器地址栏发的都是get请求
    @GetMapping(value = "/say2")//相当于上面那个  其他请求就报405  常用于读数据
    public Object say2() {
        Student student = new Student();
        student.setAge(18);
        return "student";
    }

    //    localhost:8080/say?id=1&age=20
//    localhost:8080/say/1/20   restful风格   一般要用这个风格就不用 @RequestMapping   一般入参有分页就不用restful风格  一般参数只有表字段的时候用
    @GetMapping(value = "/say3")
    public Object say3(Integer id) {
        Student student = new Student();
        student.setAge(18);
        if (id==null){return "id为空";}  //参数多传不报错
        return "student";
    }

    @RequestMapping(value = "/say4/{id}/{name}") //RESTful把参数写到请求路径里面  没参数报错，用以前方式写参数也报错 参数不够也报错
//    @RequestMapping(value = "/{id}/say4/{name}")  这样也行
    public Object say4(@PathVariable("id") Integer id,
                       @PathVariable("name") String name) {  //接收参数前加注解
        return id+name;
    }


}
